package com.glsc.ngateway.platform.service.costallocation;

import com.glsc.ngateway.platform.domain.costallocation.DepCostAllocation;
import com.glsc.ngateway.platform.domain.costallocation.HisDepCostAllocation;
import com.glsc.ngateway.platform.repo.costallocation.HisDepCostAllocationRepo;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author guoj
 * @description: 历史业务系统分摊比例
 * @date 2024/7/19 9:46
 */
@Service
public class HisDepCostAllocationService {
    @Autowired
    private HisDepCostAllocationRepo hisDepCostAllocationRepo;
    /**
     * 查询
     * @param systemIdSet
     * @return
     */
    public Map<Long, List<HisDepCostAllocation>> queryBySystemIdSet(Set<Long> systemIdSet, Map<String, Object> paramHisCostAllocation){
        if (systemIdSet == null || systemIdSet.size() == 0) {
            return new HashMap<>();
        }
        List<HisDepCostAllocation> toGroup;
        List<HisDepCostAllocation> depCostAllocations = hisDepCostAllocationRepo.queryByHisSystemInfoId(systemIdSet);
        if (paramHisCostAllocation.size() != 0) {
            List<HisDepCostAllocation> all = hisDepCostAllocationRepo.findAll(SpecificationUtil.buildSpecification(paramHisCostAllocation, HisDepCostAllocation.class));
            Set<Long> collect = all.stream().map(HisDepCostAllocation::getSystemInfoId).collect(Collectors.toSet());
            //取交集
            systemIdSet.retainAll(collect);
            toGroup = depCostAllocations.stream().filter(x -> systemIdSet.contains(x.getSystemInfoId())).collect(Collectors.toList());
        }else{
            //此时子类无查询字段  按照父类查询
            toGroup = depCostAllocations;
        }
        Map<Long, List<HisDepCostAllocation>> systemInfoId2List = toGroup.stream()
                .collect(Collectors.groupingBy(HisDepCostAllocation::getSystemInfoId));
        return systemInfoId2List;
    }
}
